// The module 'vscode' contains the VS Code extensibility API
// Import the necessary extensibility types to use in your code below
import {window, Uri,QuickPickItem, workspace, commands, Disposable, ExtensionContext, StatusBarAlignment, StatusBarItem, TextDocument} from 'vscode';
import * as path from 'path';
import * as fs from 'fs';

// This method is called when your extension is activated. Activation is
// controlled by the activation events defined in package.json.
export function activate(context: ExtensionContext) {
    function getFileName(file: string): string {
        var forwardSlash = file.lastIndexOf("/");
        var backSlash = file.lastIndexOf("\\");
        if (forwardSlash === -1 && backSlash === -1) {
            return file;
        }

        return file.substring((forwardSlash > backSlash) ? forwardSlash + 1 : backSlash + 1);
    }

    var disposable = commands.registerCommand('extension.listFilesToOpen', () => {
        if (workspace.rootPath === null){
            return;
        }

        const findListFilePath = path.join(workspace.rootPath, 'findlist');
        var config = workspace.getConfiguration("findFiles");
        var lengthToStripOff = workspace.rootPath.length + 1;
        console.log("[listfile] rootPath:" + workspace.rootPath);
        console.log("[listfile] fileIncludeGlob:" + config.get("fileIncludeGlob"));
        console.log("[listfile] fileExcludeGlob:" + config.get("fileExcludeGlob"));
        console.log("[listfile] maxResults:" + config.get("maxResults"));

          // 检查根路径下是否存在 findlist 文件
        fs.access(findListFilePath, 0, (err) => {
            if (!err) {
                // findlist 文件存在，从文件中读取文件列表
                fs.readFile(findListFilePath, 'utf8', (readErr, data) => {
                    if (readErr) {
                        window.showErrorMessage("Error reading findlist file: " + readErr.message);
                        return;
                    }

                    const fileNames = data.split('\n').map(line => line.trim()).filter(line => line.length > 0);
                    const displayFiles = fileNames.map(fileName => {
                        const filePath = path.join(workspace.rootPath, fileName);
                        return { description: filePath.substring(lengthToStripOff), label: getFileName(filePath), filePath };
                    });

                    console.log("[listfile]listfilecount from findlist:" + displayFiles.length);
                    window.showQuickPick(displayFiles).then(val => {
                        if (val) {
                            workspace.openTextDocument(val.filePath).then(d => {
                                console.log("[listfile]var.filePath:" + val.filePath);
                                window.showTextDocument(d);
                            });
                        }
                    });
                });
            } else {
                // findlist 文件不存在，走原来的流程
                workspace.findFiles(<string>config.get("fileIncludeGlob"), <string>config.get("fileExcludeGlob"), <number>config.get("maxResults")).then(files => {
                    const displayFiles = files.map(file => {
                        return { description: file.fsPath.substring(lengthToStripOff), label: getFileName(file.fsPath), filePath: file.fsPath };
                    });

                    console.log("[listfile]listfilecount from workspace.findFiles:" + displayFiles.length);
                    window.showQuickPick(displayFiles).then(val => {
                        if (val) {
                            workspace.openTextDocument(val.filePath).then(d => {
                                console.log("[listfile]var.filePath:" + val.filePath);
                                window.showTextDocument(d);
                            });
                        }
                    });
                });
            }
        });
    });
}
